# Importing Data
IRAN <- read_excel("C:/users/Biniam/Desktop/Documents/Academic/Thesis/Analysis Folder/Excel Files/Steel/TSA.xlsx",sheet = "Sheet1", range = "P1:P239")

# Checking the Imported Data
View(IRAN)
# Creating Time Series Data
IRAN_ts <- ts(IRAN, start=c(2000,1), end=c(2019,09), frequency=12)
# Viewing and Checking the Created Time Series Data
IRAN_ts
sum(is.na(IRAN_ts))
library(forecast)
IRAN_ts <- tsclean(IRAN_ts)
IRAN_ts

# Identification: Plotting the Time Series Data
plot(IRAN_ts, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = "Volume of Steel Production (in tons)", main = "Iran's Monthly Steel Production")

# Estimating the appropriate model
IRAN_ts_model <- auto.arima(IRAN_ts)
IRAN_ts_model

plot.ts(IRAN_ts_model$resid, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 2, col.axis = 4, xlab = "Years", ylab = "Residuals", main = "Residual Plot")
acf(IRAN_ts_model$residuals, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 1, col.axis = 4, xlab = "Lag", ylab = "ACF", main="ACF Residual")
pacf(IRAN_ts_model$residuals, main='ACF Residual')
Box.test(IRAN_ts_model$resid, lag=20, type="Ljung-Box")


# Forecasting
options(max.print=1000000)
IRAN_ts_forecast <- forecast (IRAN_ts_model, level=c(95), h=255)
plot(IRAN_ts_forecast, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = " Volume of Steel Production (in tons)", main = "Forecasted Monthly Steel Production for Iran")
IRAN_ts_forecast             

# Exporting
write.table(IRAN_ts_forecast, file="/users/Biniam/Desktop/Documents/Academic/Thesis/Result Folder/TSA Results/Excel Files/From R/Steel/IRAN_TSA.csv", sep=",")
